/**
 * @file  数据中转站
 * @author  hj
 */
// 引入数据请求API
import {APIS} from 'configPort';

var nativeApi = require('nativeApi');
import Router from 'vue-router';

/* 加载页面 */
import Vue from 'vue';
import index from './vue/index.vue';
import goods from './vue/goods.vue';

Vue.use(Router);

var router = new Router();
router.map({
  '/': {
    component: index
  },
  '/goods/:id': {
    name: 'goods',
    component: goods
  }
});

export default {
  state: {
    bannerData: [],
    dictionary: {},
    isNewCount: {},
    loginData: {
      username: 'admin',
      password: '111111'
    },
    icons: [],
    menuList: [],
    tips: [],
    show: true,
    count: 0,
    total: 0,
    goods: [],
    detail: {},
    // goodsPager产品列表分页加载相关数据
    goodsPager: {
      pageNo: 1,
      pageSize: 10,
      totalpages: null
    },
    // tipsPager消息列表分页加载相关数据
    tipsPager: {
      pageNo: 1,
      pageSize: 10,
      totalpages: null
    },
    catagories: [],
    indexPageSize: 10,
    indexPageNo: 1,
    indexGoods: [],
    scrolls: {
      scroll: {}
    },
    search: null,
    dictionaryGoods: [],
    myGoodsList: [],
    searchKey: {}
  },
  login: function () {
    var self = this;
    nativeApi.initAjax({
      'type': 'get',
      'url': APIS.login,
      'param': self.state.loginData,
      callback: function (result) {
        if (result && result.code === 200) {
          localStorage.setItem('login_info_test', JSON.stringify(result.data));
          router.go('/staffMenu');
        } else {
          mui.alert('登录失败' || result.msg, '提示');
        }
      }
    });
  },
  loginFromApp: function (data) {
    var self = this;
    nativeApi.initAjax({
      'type': 'get',
      'url': APIS.loginFromApp + '?' + data,
      callback: function (result) {
        if (result && result.code === 200) {
          self.getBanner();
          self.getIcons();
          self.getDictionary();
          self.getIsNewCount();
          self.getCarts();
        } else {
          mui.alert('登录失败' || result.msg, '提示');
        }
      }
    });
  },
  // logout: function () {
  //   var self = this;
  //   nativeApi.initAjax({
  //     'type': 'get',
  //     'url': APIS.logout,
  //     'param': '',
  //     callback: function (result) {
  //       if (result && result.code === 200) {
  //         localStorage.removeItem('login_info');
  //         self.state.show = true;
  //       }
  //     }
  //   });
  // },
  // getcurrentuserinfo: function () {
  //   nativeApi.initAjax({
  //     'type': 'get',
  //     'url': APIS.getcurrentuserinfo,
  //     'param': '',
  //     callback: function (result) {
  //       if (result && result.code === 200) {
  //         localStorage.setItem('login_info_test', JSON.stringify(result.data));
  //       } else {
  //         mui.alert('请登录');
  //       }
  //     }
  //   });
  // },
  getBanner: function () {
    var self = this;
    nativeApi.initAjax({
      type: 'get',
      url: APIS.Banner,
      param: {
        'bannerType': 'carousel_image'
      },
      callback: function (result) {
        if (result && result.code === 200) {
          self.state.bannerData = result.data;
        }
      }
    });
  },
  getIcons: function () {
    var self = this;
    nativeApi.initAjax({
      type: 'get',
      url: APIS.Banner,
      param: {
        'bannerType': 'index_icon_image'
      },
      callback: function (result) {
        if (result && result.code === 200) {
          self.state.icons = result.data;
        }
      }
    });
  },
  getDictionary: function () {
    var self = this;
    nativeApi.initAjax({
      type: 'get',
      url: APIS.Dictionary,
      param: {
        'dicKey': 'hot_recommend'
      },
      callback: function (result) {
        if (result && result.code === 200) {
          self.state.dictionary = result.data;
          self.getIndexGoods();
        }
      }
    });
  },
  getIsNewCount: function () {
    var self = this;
    nativeApi.initAjax({
      type: 'get',
      url: APIS.IsNewCount,
      callback: function (result) {
        if (result && result.code === 200) {
          self.state.count = result.data.count;
        }
      }
    });
  },
  getCarts: function () {
    var self = this;
    nativeApi.initAjax({
      type: 'get',
      url: APIS.Carts,
      data: {
        'cartsType': 'carousel_image'
      },
      callback: function (result) {
        if (result && result.code === 200) {
          self.state.total = result.data.total;
        }
      }
    });
  },
  addToCart: function (goodsId) {
    nativeApi.initAjax({
      type: 'post',
      url: APIS.CartList,
      param: {
        'goodsId': goodsId
      },
      callback: function (result) {
        if (result && result.code === 200) {
          mui.alert('加入购物车成功');
        } else {
          mui.alert(result.msg);
        }
      }
    });
  },
  /* 翻页相关*/
  setScroll: function (scroll) {
    this.state.scrolls.scroll = scroll;
  },
  getIndexGoods: function () {
    var self = this;
    var data = {
      'goodsType': self.state.dictionary.dicKey,
      'pageSize': self.state.indexPageSize,
      'pageNo': self.state.indexPageNo
    };
    nativeApi.initAjax({
      type: 'get',
      url: APIS.Goods,
      param: data,
      callback: function (result) {
        var endPullupToRefresh = false;
        if (result && result.code === 200) {
          self.state.indexGoods = self.state.indexGoods.concat(result.data);
          if (result.data.length >= 0) {
            self.state.indexPageNo = self.state.indexPageNo + 1;
          }
          if (result.data.length < self.state.indexPageSize) {
            endPullupToRefresh = true;
          }
        } else {
          endPullupToRefresh = true;
        }
        self.state.scrolls.scroll.endPullupToRefresh(endPullupToRefresh);
      }
    });
  },
  getIndexGoodsUP: function () {
    var self = this;
    var data = {
      'goodsType': self.state.dictionary.dicKey,
      'pageSize': self.state.indexPageSize,
      'pageNo': self.state.indexPageNo
    };
    nativeApi.initAjax({
      type: 'get',
      url: APIS.Goods,
      param: data,
      callback: function (result) {
        var endPullupToRefresh = false;
        if (result && result.code === 200) {
          result.data.forEach(function (item, index) {
            self.state.indexGoods.push(item);
          });
          if (result.data.length >= 0) {
            self.state.indexPageNo = self.state.indexPageNo + 1;
          }
          if (result.data.length < self.state.indexPageSize) {
            endPullupToRefresh = true;
          }
        } else {
          endPullupToRefresh = true;
        }
        self.state.scrolls.scroll.endPulldownToRefresh(endPullupToRefresh);
      }
    });
  },
  getDictionaryGoods: function (dictionary) {
    var self = this;
    var data = {
      'goodsType': dictionary,
      'pageSize': self.state.indexPageSize,
      'pageNo': self.state.indexPageNo
    };
    nativeApi.initAjax({
      type: 'get',
      url: APIS.Goods,
      param: data,
      callback: function (result) {
        var endPullupToRefresh = false;
        if (result && result.code === 200) {
          self.state.dictionaryGoods = self.state.dictionaryGoods.concat(result.data);
          if (result.data.length >= 0) {
            self.state.indexPageNo = self.state.indexPageNo + 1;
          }
          if (result.data.length < self.state.indexPageSize) {
            endPullupToRefresh = true;
          }
        } else {
          endPullupToRefresh = true;
        }
        self.state.scrolls.scroll.endPullupToRefresh(endPullupToRefresh);
      }
    });
  },
  getDictionaryGoodsUP: function (dictionary) {
    var self = this;
    var data = {
      'goodsType': dictionary,
      'pageSize': self.state.indexPageSize,
      'pageNo': self.state.indexPageNo
    };
    nativeApi.initAjax({
      type: 'get',
      url: APIS.Goods,
      param: data,
      callback: function (result) {
        var endPullupToRefresh = false;
        if (result && result.code === 200) {
          self.state.dictionaryGoods = result.data.concat(self.state.dictionaryGoods);
          if (result.data.length >= 0) {
            self.state.indexPageNo = self.state.indexPageNo + 1;
          }
          if (result.data.length < self.state.indexPageSize) {
            endPullupToRefresh = true;
          }
        } else {
          endPullupToRefresh = true;
        }
        self.state.scrolls.scroll.endPulldownToRefresh(endPullupToRefresh);
      }
    });
  },
  getGoods: function (dicKey, pageSize, pageNo) {
    var self = this;
    var data = {
      'goodsType': self.state.dictionary,
      'pageSize': pageSize,
      'pageNo': pageNo
    };
    nativeApi.initAjax({
      type: 'get',
      url: APIS.Goods,
      param: data,
      callback: function (result) {
        if (result && result.code === 200) {
          self.state.goodsPager.totalpages = Math.ceil(result.totalSize / self.state.goodsPager.pageSize);
          self.state.goods = self.state.goods.concat(result.data);
          self.state.goodsPager.pageNo++;
          mui('#refreshContainer').pullRefresh().endPullupToRefresh((self.state.goodsPager.pageNo > self.state.goodsPager.totalpages));
        }
      }
    });
  },
  // 进入商品详情页
  lookGoodsDetail: function (id) {
    nativeApi.initAjax({
      type: 'get',
      url: APIS.Goods + '/' + id,
      callback: function (result) {
        if (result && result.code === 200) {
          router.go({name: 'goods', params: {id: id}});
        } else {
          mui.alert(result.msg, function () {
          });
        }
      }
    });
  },
  // 获取商品详情
  getDetail: function (id) {
    var self = this;
    nativeApi.initAjax({
      type: 'get',
      url: APIS.Goods + '/' + id,
      callback: function (result) {
        if (result && result.code === 200) {
          self.state.detail = result.data;
        } else {
          mui.alert(result.msg, function () {
            window.history.go(-1);
          });
        }
      }
    });
  },
  // 获取消息列表
  getTips: function (pageSize, pageNo) {
    var self = this;
    nativeApi.initAjax({
      type: 'get',
      url: APIS.tips + '?pageSize=' + pageSize + '&pageNo=' + pageNo,
      callback: function (result) {
        if (result && result.code === 200) {
          self.state.tipsPager.totalpages = Math.ceil(result.totalSize / self.state.tipsPager.pageSize);
          self.state.tips = self.state.tips.concat(result.data);
          self.state.tipsPager.pageNo++;
          mui('#refreshContainer').pullRefresh().refresh(true);
          mui('#refreshContainer').pullRefresh().endPullupToRefresh((self.state.tipsPager.pageNo > self.state.tipsPager.totalpages));
        }
      }
    });
  },
  // 获取分类
  getCatagories: function (keyword) {
    var self = this;
    var data = {};
    if (keyword) {
      data.keyword = keyword;
    }
    nativeApi.initAjax({
      type: 'get',
      url: APIS.getCatagories,
      param: data,
      callback: function (result) {
        if (result && result.code === 200) {
          self.state.catagories = result.data;
        }
      }
    });
  },
  getMyGoods: function (searchKey, pageSize, pageNo) {
    var self = this;
    var data = {
      pageNo: pageNo,
      pageSize: pageSize
    };
    if (searchKey.goodsType) {
      data.goodsType = searchKey.goodsType;
    } else if (searchKey.catagoryId) {
      data.catagoryId = searchKey.catagoryId;
    } else if (searchKey.keyword) {
      data.keyword = searchKey.keyword;
    }
    nativeApi.initAjax({
      type: 'get',
      url: APIS.Goods,
      param: data,
      callback: function (result) {
        if (result && result.code === 200) {
          self.state.goodsPager.totalpages = Math.ceil(result.totalSize / self.state.goodsPager.pageSize);
          self.state.myGoodsList = self.state.myGoodsList.concat(result.data);
          self.state.goodsPager.pageNo++;
          mui('#refreshContainer').pullRefresh().refresh(true);
          if (result.totalSize === 0) {
            mui('#refreshContainer').pullRefresh().disablePullupToRefresh();
          }
          mui('#refreshContainer').pullRefresh().endPullupToRefresh((self.state.goodsPager.pageNo > self.state.goodsPager.totalpages));
        }
      }
    });
  },
  // 修改消息状态
  putTipChange: function (id) {
    nativeApi.initAjax({
      type: 'put',
      url: APIS.tips + '/' + id,
      callback: function (result) {
        if (result && result.code === 200) {
          console.log(result.msg);
        }
      }
    });
  },
  // 兑换商品
  exchangeGoods: function () {
  }
};
